﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;

namespace ImageConvert
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string[] _supported;
        private ObservableCollection<string> _Files;
        private Conversion _convwin;

        public ObservableCollection<string> Files
        { 
            get { return _Files; }
            set { _Files = value; }
        }

        public MainWindow()
        {
            _Files = new ObservableCollection<string>();
            InitializeComponent();
            App.Win = new Windows();
            App.AppOptions = new Options();
            App.MainWin = this;
            _convwin = new Conversion();
            DataContext = this;
            _supported = new string[]
            {
                "*.jpg", "*.jpeg", "*.bmp", "*.png", ".tiff"
            };
        }

        private void FileExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void InputAdd_Click(object sender, RoutedEventArgs e)
        {
            string[] f = App.Win.OpenFiles(null);
            if (f != null)
            {
                foreach (var file in f)
                {
                    _Files.Add(file);
                }
            }
        }

        private void InputAddDir_Click(object sender, RoutedEventArgs e)
        {
            string dir = App.Win.OpenDirectory(null);
            if (string.IsNullOrEmpty(dir)) return;
            List<string> files = new List<string>();
            foreach (var filter in _supported)
            {
                files.AddRange(Directory.GetFiles(dir, filter));
            }
            files.Sort();
            foreach (var file in files)
            {
                _Files.Add(file);
            }

        }

        private void InputAddDirr_Click(object sender, RoutedEventArgs e)
        {
            string dir = App.Win.OpenDirectory(null);
            if (string.IsNullOrEmpty(dir)) return;
            List<string> files = new List<string>();
            foreach (var filter in _supported)
            {
                files.AddRange(Directory.GetFiles(dir, filter, SearchOption.AllDirectories));
            }
            files.Sort();
            foreach (var file in files)
            {
                _Files.Add(file);
            }
        }

        private void InputClear_Click(object sender, RoutedEventArgs e)
        {
            _Files.Clear();
        }

        private void InputRemove_Click(object sender, RoutedEventArgs e)
        {
            _Files.RemoveAt(LbInFiles.SelectedIndex);
        }

        private void BtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            string ipath = null;
            if (!string.IsNullOrEmpty(TbOutDir.Text)) ipath = TbOutDir.Text;
            string dir = App.Win.OpenDirectory(ipath);
            TbOutDir.Text = dir;
        }

        private void BtnConvert_Click(object sender, RoutedEventArgs e)
        {
            App.AppOptions.Height = (int)SlideResHeight.Value;
            App.AppOptions.Percent = (int)SlideResPercent.Value;
            App.AppOptions.Quality = (int)SlideFormatQuality.Value;
            App.AppOptions.Width = (int)SlideResWidth.Value;
            App.AppOptions.TargetDir = TbOutDir.Text;

            if ((bool)RbResFitbox.IsChecked) App.AppOptions.SizeMode = SizeMode.BoxFit;
            else if ((bool)RbResPercent.IsChecked) App.AppOptions.SizeMode = SizeMode.Percent;
            else App.AppOptions.SizeMode = SizeMode.NoChange;

            if ((bool)RbFormJpg.IsChecked) App.AppOptions.OutputFormat = Format.Jpeg;
            else if ((bool)RbFormPng.IsChecked) App.AppOptions.OutputFormat = Format.Png;
            else if ((bool)RbFormBmp.IsChecked) App.AppOptions.OutputFormat = Format.Bmp;
            else if ((bool)RbFormTiff.IsChecked) App.AppOptions.OutputFormat = Format.Tiff;

            if ((bool)RbColorNo.IsChecked) App.AppOptions.PixelFormat = PixFormat.Nochange;
            else if ((bool)RbColor8bit.IsChecked) App.AppOptions.PixelFormat = PixFormat.Nochange;
            else if ((bool)RbColor24bit.IsChecked) App.AppOptions.PixelFormat = PixFormat.Bit24;
            else if ((bool)RbColor32bit.IsChecked) App.AppOptions.PixelFormat = PixFormat.Bit32;

            _convwin.SetupThreads(_Files);
            _convwin.ConvMode();
            _convwin.StartThreads();
        }

        private void MainWin_Loaded(object sender, RoutedEventArgs e)
        {
            RbResNochange.IsChecked = true;
            RbFormJpg.IsChecked = true;
            RbColorNo.IsChecked = true;
        }

        private void MainWin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _convwin.Close();
        }
    }
}
